//
//  $Id: MonitorInfoOverlayView.m 2568 2016-09-30 07:53:44Z zhao_kunpeng $
//  smarthome
//
//  Created by hu_danyuan on 14-9-17.
//   (c) Copyright 2014,  杭州泺平电子有限公司
//                            All Rights Reserved
//
//
//  描    述:  视频监控中视频信息overlay
//

#import "MonitorInfoOverlayView.h"
#import <VCS_WS_ClientSDK/VCS_WS_VideoFrameInfo.h>
#import <VCS_WS_ClientSDK/VCS_WS_AudioFrameInfo.h>

#define TRAFFIC_STATISTICAL_INTERVAL 3

@interface MonitorInfoOverlayView ()

// 分辨率
@property (nonatomic, retain) UILabel   *resolutionLabel;
@property (nonatomic, retain) UILabel   *originResolutionLabel;
// 流量
@property (nonatomic, retain) UILabel   *kbsLabel;
@property (nonatomic, assign) NSUInteger    kbs;
// 速率
@property (nonatomic, assign) NSUInteger    kbps;
// 统计定时器
@property (nonatomic, retain) NSTimer       *kbpsTimer;
@property (nonatomic, assign) CGFloat       kbs_ns;
@property (nonatomic, assign) CGFloat       kbps_ns;

@end

@implementation MonitorInfoOverlayView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.kbs = 0;
        self.kbs_ns = 0;
        self.kbps_ns = 0;
        self.backgroundColor = [UIColor clearColor];
        // 分辨率
        CGRect rect = CGRectMake(10, 80, frame.size.width, 20);
        self.resolutionLabel = [[UILabel alloc] initWithFrame:rect];
        self.resolutionLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        self.resolutionLabel.backgroundColor = [UIColor clearColor];
        self.resolutionLabel.textColor = [UIColor whiteColor];
        self.resolutionLabel.font = [UIFont systemFontOfSize:12];
        self.resolutionLabel.textAlignment = NSTextAlignmentLeft;
        [self addSubview:self.resolutionLabel];

        // 分辨率
        rect.origin.y += rect.size.height;
        self.originResolutionLabel = [[UILabel alloc] initWithFrame:rect];
        self.originResolutionLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        self.originResolutionLabel.backgroundColor = [UIColor clearColor];
        self.originResolutionLabel.textColor = [UIColor whiteColor];
        self.originResolutionLabel.font = [UIFont systemFontOfSize:12];
        self.originResolutionLabel.textAlignment = NSTextAlignmentLeft;
        [self addSubview:self.originResolutionLabel];

        // 流量
        rect.origin.y += rect.size.height;
        self.kbsLabel = [[UILabel alloc] initWithFrame:rect];
        self.kbsLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        self.kbsLabel.backgroundColor = [UIColor clearColor];
        self.kbsLabel.textColor = [UIColor whiteColor];
        self.kbsLabel.font = [UIFont systemFontOfSize:12];
        self.kbsLabel.textAlignment = NSTextAlignmentLeft;
        [self addSubview:self.kbsLabel];
    }
    return self;
}

- (void)dealloc
{
    self.resolutionLabel = nil;
    self.kbsLabel = nil;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (NSString *)byteSizeToString:(uint64_t)byteSize
{
    NSString *ret = nil;
    if (byteSize >= (0x01 << 20)) {
        double flow = byteSize / (1.0 * (0x01 << 20));
        ret = [NSString stringWithFormat:@"%0.2lfM", flow];
    } else {
        double flow = byteSize / (1.0 * (0x01 << 10));
        ret = [NSString stringWithFormat:@"%0.2lfKB", flow];
    }
    return ret;
}

- (void)makeTrafficForStatistical
{
    [self makeTrafficForStatistical:YES];
}

- (void)makeTrafficForStatistical:(BOOL)calculateKBPS
{
    if (!calculateKBPS) {
        NSString *kbpsText = [NSString stringWithFormat:@"%0.2lfKB/s", self.kbps_ns];
        self.kbsLabel.text = [NSLocalizedString(@"Traffic", nil) stringByAppendingFormat:@": %@   %@", [self byteSizeToString:self.kbs], kbpsText];
    } else {
        CGFloat kbps = self.kbs_ns / (TRAFFIC_STATISTICAL_INTERVAL * 1024.0);
        NSString *kbpsText = [NSString stringWithFormat:@"%0.2lfKB/s", kbps];
        if (self.kbsLabel.text == nil) {
            self.kbsLabel.text = kbpsText;
        } else {
            self.kbsLabel.text = [NSLocalizedString(@"Traffic", nil) stringByAppendingFormat:@": %@   %@", [self byteSizeToString:self.kbs], kbpsText];
        }
        self.kbs_ns = 0.0;
        self.kbps_ns = kbps;
    }
}

- (void)startRecord
{
    [self stopRecord];
    self.kbpsTimer = [NSTimer scheduledTimerWithTimeInterval:TRAFFIC_STATISTICAL_INTERVAL target:self selector:@selector(makeTrafficForStatistical) userInfo:nil repeats:YES];
}

- (void)stopRecord
{
    [self.kbpsTimer invalidate];
    self.kbpsTimer = nil;
}

- (NSString *)frameInfoTextFrom:(VCS_WS_VideoFrameInfo *)frameInfo
{
    NSString *encodeStr = nil;
    if (frameInfo.encodeType == VCS_WS_VideoEncodeMpeg4)
        encodeStr = @"Mpeg4";
    else if (frameInfo.encodeType == VCS_WS_VideoEncodeH264)
        encodeStr = @"H264";
    else if (frameInfo.encodeType == VCS_WS_VideoEncodeH265)
        encodeStr = @"H265";
    else
        encodeStr = [NSString stringWithFormat:@"%zi", frameInfo.encodeType];
    NSString *resolution = [NSString stringWithFormat:@"%@: %d x %d", NSLocalizedString(@"分辨率", nil), frameInfo.width, frameInfo.height];
    resolution = [resolution stringByAppendingFormat:@", %dfps, \tencode: %@", frameInfo.frameRate, encodeStr];
    return resolution;
}

- (NSString *)audioInfoTextFrom:(VCS_WS_AudioFrameInfo *)frameInfo
{
    NSString *encodeStr = nil;
    if (frameInfo.encodeType == VCS_WS_AudioEncodeTypeG711A)
        encodeStr = @"G711a";
    else if (frameInfo.encodeType == VCS_WS_AudioEncodeTypeG711U)
        encodeStr = @"G711u";
    else if (frameInfo.encodeType == VCS_WS_AudioEncodeTypeG7221)
        encodeStr = @"G722.1";
    else if (frameInfo.encodeType == VCS_WS_AudioEncodeTypeG726)
        encodeStr = @"G726";
    else if (frameInfo.encodeType == VCS_WS_AudioEncodeTypeAAC)
        encodeStr = @"AAC";
    else if (frameInfo.encodeType == VCS_WS_AudioEncodeTypePCM)
        encodeStr = @"PCM";
    else if (frameInfo.encodeType == VCS_WS_AudioEncodeTypeSVAC)
        encodeStr = @"SVAC";
    else
        encodeStr = [NSString stringWithFormat:@"%zi", frameInfo.encodeType];
    NSString *resolution = [NSString stringWithFormat:@"%@: %dHz, %@: %dbit, \tencode: %@", NSLocalizedString(@"采样率", nil), frameInfo.sampleRate, NSLocalizedString(@"位深", nil), frameInfo.bitsPerSample, encodeStr];
    return resolution;
}

- (void)setFrameInfo:(NSObject *)frameInfo originFrameInfo:(NSObject *)originFrameInfo frameSize:(NSUInteger)frameSize
{
    NSAssert([[NSThread currentThread] isMainThread], @"<MonitorInfoOverlayView@setFrameInfo:frameSize:> This method must be called on the main thread.");

    self.kbs += frameSize;
    self.kbs_ns += frameSize;
    [self makeTrafficForStatistical:NO];
    if ([frameInfo isKindOfClass:[VCS_WS_VideoFrameInfo class]]) {
        VCS_WS_VideoFrameInfo *frameInfo_ = (VCS_WS_VideoFrameInfo *)frameInfo;
        self.resolutionLabel.text = [self frameInfoTextFrom:frameInfo_];
        //self.originResolutionLabel.text = [NSString stringWithFormat:@"%@%@", NSLocalizedString(@"原始", nil), [self frameInfoTextFrom:(VCS_WS_VideoFrameInfo *)originFrameInfo]];
    } else if ([frameInfo isKindOfClass:[VCS_WS_AudioFrameInfo class]]) {
        VCS_WS_AudioFrameInfo *frameInfo_ = (VCS_WS_AudioFrameInfo *)frameInfo;
        self.originResolutionLabel.text = [self audioInfoTextFrom:frameInfo_];
    } else {
        NSAssert(0, @"<MonitorInfoOverlayView@setFrameInfo:frameSize:>Unknown frame info");
    }
}

@end
